package com.example.demo.controller;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HbaseTestController {

    @Autowired
    private HbaseTemplate hbaseTemplate;

    @Autowired
    private Connection connection;

    @GetMapping("/test")
    private Boolean testtet() throws IOException {
        System.out.println("jjjjj");
        final Admin admin = connection.getAdmin();
        final boolean yaoce_test = admin.tableExists(TableName.valueOf("yaoce_test"));
        return yaoce_test;
    }

    @PostMapping("/test1")
    private Boolean test1(@RequestBody Map<String, String> params) throws IOException {
        System.out.println(params);
        final Admin admin = connection.getAdmin();
        final String tableName = params.get("tableName");
        final boolean yaoce_test = admin.tableExists(TableName.valueOf(tableName));
        return yaoce_test;
    }

    @PostMapping("/test2")
    private Boolean test2(@RequestBody String params) throws IOException {
        System.out.println(params);
        final ObjectMapper objectMapper = new ObjectMapper();
        final JsonNode jsonNode = objectMapper.readTree(params);
        final Admin admin = connection.getAdmin();
        final String tableName = jsonNode.get("tableName").asText();
        final boolean yaoce_test = admin.tableExists(TableName.valueOf(tableName));
        return yaoce_test;
    }

    @GetMapping("/test3")
    private List<Map<String, Object>> test3() {
        final Scan scan = new Scan();
        final List<Map<String, Object>> yaoce_test = hbaseTemplate.find("yaoce_test", scan, new RowMapper<Map<String, Object>>() {
            public Map<String, Object> mapRow(Result result, int rowNum) throws Exception {
                final byte[] eq_no = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("eq_no"));
                final byte[] kuixian = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("kuixian"));
                final byte[] createtime = result.getValue(Bytes.toBytes("time"), Bytes.toBytes("createtime"));
                final HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                stringObjectHashMap.put("eq_no", new String(eq_no));
                stringObjectHashMap.put("kuixian", new String(kuixian));
                final long aLong = ByteBuffer.wrap(createtime).getLong();
                stringObjectHashMap.put("createtime", aLong);

                return stringObjectHashMap;
            }
        });
        System.out.println(yaoce_test);
        return yaoce_test;
    }


    @GetMapping("/test4")
    private List<Map<String, Object>> test4() {
        final Scan scan = new Scan();
//        new Filter();
        final SingleColumnValueFilter singleColumnValueFilter =
                new SingleColumnValueFilter(
                        Bytes.toBytes("info"),
                        Bytes.toBytes("kuixian"),
                        CompareFilter.CompareOp.EQUAL, Bytes.toBytes("10kV汇水一线956"));
        final Scan scan1 = scan.setFilter(singleColumnValueFilter);
        final List<Map<String, Object>> yaoce_test = hbaseTemplate.find("yaoce_test", scan, new RowMapper<Map<String, Object>>() {
            public Map<String, Object> mapRow(Result result, int rowNum) throws Exception {
                final byte[] eq_no = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("eq_no"));
                final byte[] kuixian = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("kuixian"));
                final byte[] createtime = result.getValue(Bytes.toBytes("time"), Bytes.toBytes("createtime"));
                final HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                stringObjectHashMap.put("eq_no", new String(eq_no));
                stringObjectHashMap.put("kuixian", new String(kuixian));
                return stringObjectHashMap;
            }
        });
        System.out.println(yaoce_test);
        return yaoce_test;
    }


    @GetMapping("/test5")
    private List<Map<String, Object>> test5() {
        final Scan scan = new Scan();
        final SingleColumnValueFilter singleColumnValueFilter =
                new SingleColumnValueFilter(
                        Bytes.toBytes("info"),
                        Bytes.toBytes("seq"),
                        CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes.toBytes("##20"));


        final SingleColumnValueFilter singleColumnValueFilter1 =
                new SingleColumnValueFilter(
                        Bytes.toBytes("info"),
                        Bytes.toBytes("seq"),
                        CompareFilter.CompareOp.LESS_OR_EQUAL, Bytes.toBytes("##100"));

        final FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filterList.addFilter(singleColumnValueFilter);
        filterList.addFilter(singleColumnValueFilter1);

        final Scan scan1 = scan.setFilter(filterList);
        final List<Map<String, Object>> yaoce_test = hbaseTemplate.find("yaoce_test", scan, new RowMapper<Map<String, Object>>() {
            public Map<String, Object> mapRow(Result result, int rowNum) throws Exception {
                final byte[] eq_no = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("eq_no"));
                final byte[] kuixian = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("kuixian"));
                final byte[] createtime = result.getValue(Bytes.toBytes("time"), Bytes.toBytes("createtime"));
                final HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                stringObjectHashMap.put("eq_no", new String(eq_no));
                stringObjectHashMap.put("kuixian", new String(kuixian));
                return stringObjectHashMap;
            }
        });
        System.out.println(yaoce_test.size());
        return yaoce_test;
    }
}
